package com.example.demo.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/** @author Hu yujing */
public class SplitWithCommaUtils {
  private static final String CHINESE_COMMA = "，";
  private static final String ENGLISH_COMMA = ",";
  private final String numberString;

  /** @param numberString 待处理的单号字符串 */
  public SplitWithCommaUtils(String numberString) {
    this.numberString = numberString;
  }

  /**
   * 获取被过滤的单号列表 按逗号分割单号，然后转换为List, 过滤出数组元素非空的元素
   *
   * @return 非空的单号列表
   */
  public List<String> getBeFilteredNumbers() {
    return splitCommaAsList().stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
  }
  /**
   * 按逗号分割单号，然后转换为List
   *
   * @return 单号列表
   */
  private List<String> splitCommaAsList() {
    String trimCodesString = numberString.trim();
    String replaceCodesString = trimCodesString.replaceAll(CHINESE_COMMA, ENGLISH_COMMA);
    // 英文符号使用英文符号进行分割
    if (replaceCodesString.contains(ENGLISH_COMMA)) {
      return Arrays.asList(replaceCodesString.split(ENGLISH_COMMA));
    }
    // 没有逗号的话，即为单个订单号的情况
    return Collections.singletonList(replaceCodesString);
  }
}
